﻿namespace MicroCloud.Extensions
{
    #region "<see cref="IFunction"/> 扩展操作类"
    /// <summary>
    /// <see cref="IFunction"/> 扩展操作类
    /// </summary>
    public static class FunctionExtensions
    {
        #region "方法"
        #region "将 <see cref="IFunction"/> 的缓存配置转换为 <see cref="DistributedCacheEntryOptions"/>"
        /// <summary>
        /// 将 <see cref="IFunction"/> 的缓存配置转换为 <see cref="DistributedCacheEntryOptions"/>
        /// </summary>
        public static DistributedCacheEntryOptions ToCacheOptions(this IFunction function)
        {
            if (function == null)
            {
                return null;
            }
            if (function.CacheExpirationSeconds == 0)
            {
                return null;
            }
            DistributedCacheEntryOptions options = new();
            if (!function.IsCacheSliding)
            {
                options.SetAbsoluteExpiration(TimeSpan.FromSeconds(function.CacheExpirationSeconds));
            }
            else
            {
                options.SetSlidingExpiration(TimeSpan.FromSeconds(function.CacheExpirationSeconds));
            }
            return options;
        }
        #endregion

        #endregion

    }
    #endregion

}
